<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-7-16
 * Time: 上午9:35
 */
include "/var/www/html/new/d/module/qkxproject/api/des/des.class.php";
class Zushan
{
    function __construct($UUlid)
    {
        if($UUlid==9062){
        $this->url = 'http://fx.zushan.net/index.php/public/Piaocenter/';
        }elseif($UUlid==9060||$UUlid==9395||$UUlid==7863||$UUlid==10680){
          $this->url = 'http://pw.yudao.co/index.php/public/Piaocenter/';

        }
        $this->time = time();
        $this->date = date('Y-m-d,H:i:s');
        switch($UUlid){
            case 9062://海源-祖山
                 $this->user = '13333355000';
                 $this->authcode = '9qtYxsle9ElyLwBmPoZQBiBq';
                 $this->pwd = '5866658666';
                 $this->businessid='35e5296cea7ec09d1e2def9c15f6c8ce';

         break;
             case 9060: //海源—渔岛

                $this->user = '13780366666';
                $this->authcode = 'khADwHgpcxszE9QlUDmdMnap';
                $this->pwd = '5866658666';
                $this->businessid='4af3869c9529544eb71fe2f02309b0ae';

                break;
            case  9395://鸿利-渔岛
                $this->user = '13803352069';
                $this->authcode = 'a50VoJktKFoBan0FO7IL6lkf';
                $this->pwd = '703218';
                $this->businessid='9075ad97f5767bf8d94995b6eb35e37c';
                break;
            case 10680://百合-渔岛
                $this->user = '13933533445';
                $this->authcode = 'Efynu34t45kV43mRUtYDgzel';
                $this->pwd = '850589';
                $this->businessid='b41c8c971ff12496e8de60e989ea4e2a';
             break;
        default:
            break;
    }

}

    function SubmitOrder($request_array)
    {
       $request_data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Body>
  <timeStamp>{$this->time}</timeStamp>
  <user>{$this->user}</user>
  <goodsId>{$request_array['uuid']}</goodsId>
  <people>{$request_array['UUtnum']}</people>
  <num>1</num>
  <phone>{$request_array['UUordertel']}</phone>
  <guest_name>{$request_array['pftordername']}</guest_name>
  <identityno>{$request_array['IDcar']}</identityno>
  <price>{$request_array['UUtprice']}</price>
  <order_note>订单备注</order_note>
  <forecasttime>$this->date</forecasttime>
  <outOrderId>{$request_array['pftordernum']}</outOrderId>
  <orderpost>
    <consignee>收货地址(收货人)</consignee>
    <province>收货地址(省)</province>
    <city>收货地址(市)</city>
    <district>收货地址(县)</district>
    <address>收货地址(具体地址)</address>
    <zipcode>收货地址(区号)</zipcode>
    </orderpost>
</Body>

XML;



        $a= new Crypt3Des($this->authcode);
        $content = $a->encrypt($request_data);

        $array=array(
            'businessid'=>$this->businessid,
            'content'=>$content
        );

        $url = $this->url.'SubmitOrder?';
//       logfile((string)$url);
        $post_request = $this->curl_post($url, $array);

        $post_request_xml =$a->decrypt($post_request);
        $request_data_xml = simplexml_load_string($post_request_xml);

        $credence = (string)$request_data_xml->credence;
        $status = (string)$request_data_xml->status;
        $outOrderId = (string)$request_data_xml->outOrderId;
        if($status ==0){
            return $credence;
        } else{
             $this->logfile(date('Y-m-d H:i:s').'---'.'outOrderId'.$outOrderId.'---'.'status'.":".$status);
            return 101;
            }

    }

     public  function  BackOrder($arr_refund){

   $request_data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Body>
   <timeStamp>$this->time</timeStamp>
   <user>{$this->user}</user>
   <credenceno>{$arr_refund['UUremotenum']}</credenceno>
   <backNum>{$arr_refund['UUtnum']}</backNum>
   <outBackId>{$arr_refund['orders']}</outBackId>
</Body>
XML;
//        $this->logfile('$request_data'.$request_data);

         $a= new Crypt3Des($this->authcode);
         $content = $a->encrypt($request_data);

         $array=array(
             'businessid'=>$this->businessid,
             'content'=>$content
         );
         $url = $this->url.'ShortBackOrder?';
         $post_request = $this->curl_post($url, $array);
         $post_request_xml =$a->decrypt($post_request);
         $request_data_xml = simplexml_load_string($post_request_xml);
//         $this->logfile('$request_data_xml'.json_encode($request_data_xml));
//         $this->logfile((int)$request_data_xml->status);
         $status = (int)$request_data_xml->status;


         if( $status ==0){
            return 100;
         }else{
             $this->logfile(date('Y-m-d H:i:s').'---'.'outOrderId'.'---'.$arr_refund['orders'].'---'.'status'.":".$status);
             return 101;
         }

     }
    public function  curl_post($url, $array)
    {
        $data = http_build_query($array);

//       echo $data;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
    function logfile($txt)
    {
        $fp = fopen('/var/www/html/new/d/module/qkxproject/api/zushan/zushan_log.txt', "a");
        flock($fp, LOCK_EX);
        fwrite($fp, $txt . "\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }
}